from flask import current_app
from apps.interface.models.interfacecaserelation import InterfaceCaseRelation
from apps.interface.models.interfacecasedata import InterfaceCaseData
from apps.interface.util.utils import *
from library.api.db import db


class InterfaceCaseRelationBusiness(object):
    
    @classmethod
    def handle_interface_case(cls, case_id, steps, _temp_config):
        """处理接口用例的步骤
        :param case_id:用例id
        :param steps:步骤
        :param _temp_config:配置
        :return:
        """
        # 关联用例
        case_steps = InterfaceCaseRelation.query.filter_by(relation_case_id=case_id,
                                                           execute_status=0).all()
        # 排序
        steps = calculation_case_step_order(case_steps, steps)
        base_steps = []
        for step in steps:
            # 步骤
            if step.type != 'action':
                base_steps.append(step)
            # 用例
            else:
                case_step = InterfaceCaseData.query.filter_by(
                    case_id=step.case_id,
                    execute_status=InterfaceCaseData.ACTIVE).order_by(
                    InterfaceCaseData.num.asc()
                ).all()
                # 遍历用例下步骤
                for i in range(len(case_step)):
                    base_steps.append(case_step[i])
        
        return base_steps
    
    @classmethod
    def create_case_relation(cls, num, case_id, action):
        """创建接口与模块用例关联关系
        :param num:排序
        :param case_id:关联用例id
        :param action:关联用例信息
        :return:
        """
        try:
            m = InterfaceCaseRelation(
                case_id=action.get('case_id'),
                relation_case_id=case_id,
                num=num,
                times=action.get('time'),
                type=action.get('type'),
                status=json.dumps(action.get('status')),
                case_name=action.get('case_name'),
            )
            db.session.add(m)
            db.session.commit()
            return None
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(str(e))
            return 102, str(e)
    
    @classmethod
    def update_case_relation(cls, num, c_id, action):
        """修改接口与模块用例关联关系
        :param num:排序
        :param c_id:关联用例id
        :param action:关联用例信息
        :return:
        """
        try:
            relation_id = action.get('id')
            m = InterfaceCaseRelation.query.get(relation_id)
            m.num = num,
            m.case_id = action.get('case_id'),
            m.relation_case_id = c_id,
            m.times = action.get('time'),
            m.status = json.dumps(action.get('status')),
            m.case_name = action.get('case_name'),
            db.session.add(m)
            db.session.commit()
            return None
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(str(e))
            return 102, str(e)
    
    @classmethod
    def del_case(cls, relation_id):
        """删除用例下关联用例关系
        :param relation_id:引用用例id
        :return:
        """
        relation_data = InterfaceCaseRelation.query.filter_by(id=relation_id).first()
        if not relation_data:
            raise Exception('用例id不存在')
        m = InterfaceCaseRelation.query.get(relation_id)
        m.execute_status = 1
        db.session.add(m)
        db.session.commit()
        return '删除引用用例成功'
    
    @classmethod
    def del_relation_case(cls, relation_id):
        """删除用例下关联用例关系
        :param relation_id:引用用例id
        :return:
        """
        relation_data = InterfaceCaseRelation.query.filter_by(case_id=relation_id).all()
        if not relation_data:
            return '用例id不存在'
        # 删除所有关联信息
        for relation_case in relation_data:
            m = InterfaceCaseRelation.query.get(relation_case.id)
            m.execute_status = 1
            db.session.add(m)
        db.session.commit()
        return '删除用例信息关联关系成功'
    
    @classmethod
    def get_case_var(cls, relation_case_id):
        """获取接口用例关联的用例信息的id
        :param relation_case_id:关联用例id
        :return:case_id
        """
        relation_data = InterfaceCaseRelation.query.filter_by(
            relation_case_id=relation_case_id).all()
        if relation_data:
            case_id = list(set([case_relation.case_id for case_relation in relation_data]))
            return case_id
        return []
